<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-10-05 14:55:13
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\Pro;


use App\Http\Services\Service;
use App\Repositories\ProBomRepository;
use App\Repositories\ProBomDetailRepository;
use DB;

/**
 * @property ProBomRepository $repository
 * @property ProBomDetailRepository $details 
 */
class BomService extends Service
    {
    public function boot(
        ProBomRepository $repository,
        ProBomDetailRepository $details,
    ) {
        $this->repository = $repository;
        $this->details    = $details;
        }

    public function list()
        {
        // DB::enableQueryLog();
        $data = $this->repository->condition(request())->getGrid();
        // dd(DB::getQueryLog());

        return $data;
        }
    public function bomlist()
        {
        // DB::enableQueryLog();

        $request = request();
        // $request->offsetSet("table_id", "397");
        // $filter = $request->input('filter');

        // // 检查路径是否存在，并移除字段
        // if (
        //     isset($filter[2]['details.material.film_category_id'])
        // ) {
        //     unset($filter[2]['details.material.film_category_id']);
        // }

        // // 将修改后的 filter 合并回请求
        // $request->merge(['filter' => $filter]);
        
        $data = $this->repository->condition($request)->limit(40)->getList();
        // dd(sql());
        return $data;
        }

    public function details()
        {
        $data = $this->details->condition(request())->getGrid();
        return $data;
        }


    protected function save()
        {
        $data = request()->toArray();


        return $this->repository->validate($data)->autosave($data);
        }
    protected function delete($request)
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            if ($v->status_id != -1) {
                fail("启用状态无法删除！");
                }
            $v->details->filter(function ($vv)
                {
                $vv->delete();
                });
            $v->delete();
            });
        }
    protected function audit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            auditSave($v, true, true, 0);
            });
        }

    protected function noaudit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            auditSave($v, false, true, -1);
            });
        }

    }